/*****************************************************************************
 *
 *                      HUSONG PROPRIETARY INFORMATION
 *
 *          The information contained herein is proprietary to HUSONG
 *           and shall not be reproduced or disclosed in whole or in part
 *                    or used for any design or manufacture
 *              without direct written authorization from HUSONG.
 *
 *            Copyright (c) 2015 by HUSONG.  All rights reserved.
 *
 *****************************************************************************/
package com.broker.provider.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import me.send.broker.entity.Opencity;
import me.send.broker.service.OpenCityService;
import me.send.business.base.ErrorEntity;
import me.send.business.base.SendVariable;
import me.send.framework.util.BaseMap;

import com.broker.provider.base.BaseService;

/**
 * @author maxmin
 *
 * @date 2015年8月27日 上午10:04:53
 */
public class OpenCityServiceImpl extends BaseService implements OpenCityService {

	// 使用静态的成员变量 来加
	private static final String OPENCITY_LETTER = "A|B|C|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|T|U|V|W|X|Y|Z";
	private static final int OPEN_CITY_STATUS_OPEN = 1;

	/*
	 * (non-Javadoc)
	 * 
	 * @see me.send.broker.service.OpenCityService#list_all(java.lang.String)
	 */
	public Map<String, Object> list_all(String devicenumber) {
		// 实例化一个map 返回对象
		Map<String, Object> result = new HashMap<String, Object>();

		// 判断这个device
		// 返回下面的错误信息
		ErrorEntity entity = getErrorEntity("1000");
		// 取出当前开放的城市 2015年6月9日09:41:06
		List<Map<String, Object>> open_list = new ArrayList<Map<String, Object>>();
		// 取出下面待开放城市 2015年6月9日09:43:28 wait 待
		List<Map<String, Object>> wait_list = new ArrayList<Map<String, Object>>();
		if (isDevicenumber(devicenumber)) {
			// 开放状态是 0 的表示还没有开放的城市
			open_list = opencityMapper.list_openstatus(OPEN_CITY_STATUS_OPEN);
			// 开放的状态是 1的 表示已经开放的城市

			Opencity opencity = new Opencity();
			opencity.setOpenstatus(0);
			// 变量
			// List<Map<String, Object>> list = new
			// ArrayList<Map<String,Object>>();

			String[] opencityLetters = OPENCITY_LETTER.split("\\|");
			for (String letter : opencityLetters) {
				opencity.setLetter(letter);
				Map<String, Object> resultMap = new HashMap<String, Object>();
				resultMap.put("opencity",
						opencityMapper.list_opencity(opencity));
				wait_list.add(resultMap);
			}

			// open_list = opencityMapper.list_openstatus(1);
			// 错误信息等 成功
			// entity = error_1000();
		} else {
			entity = getErrorEntity("1002");
		}
		result.put(SendVariable.COMM_ERROR, entity); // 将错误的信息放在返回结果里面
		// 最后返回这个对象
		result.put("wait_list", wait_list);
		result.put("open_list", open_list);
		return result;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * me.send.broker.service.OpenCityService#citiesForAndroid(java.lang.String)
	 */
	public Map<String, Object> citiesForAndroid(String devicenumber) {
		// 实例化一个map 返回对象
		Map<String, Object> result = new HashMap<String, Object>();
		List<Map<String, Object>> open_list = new ArrayList<Map<String, Object>>();
		List<Map<String, Object>> wait_list = new ArrayList<Map<String, Object>>();
		open_list = opencityMapper.list_openstatus(OPEN_CITY_STATUS_OPEN);
		wait_list = opencityMapper.citiesAll();
		result.put("openList", open_list);
		result.put("waitList", wait_list);
		return result;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * me.send.broker.service.OpenCityService#location_cityname(java.lang.String
	 * , java.lang.String, java.lang.String)
	 */
	public Map<String, Object> location_cityname(String devicenumber, String x,
			String y) {
		Map<String, Object> map = new HashMap<String, Object>();
		// 判断是否有keyt
		ErrorEntity entity = getErrorEntity("1000");
		Map<String, Object> map3 = new HashMap<String, Object>();
		if (isDevicenumber(devicenumber)) {
			// 获取百度地图的 城市名称
			String cityname = BaseMap.GetLocationCityGeocoding(x, y);
			if (cityname != null && !cityname.equals("")) {
				// 判断cityname是否是null
				Map<String, Object> map2 = opencityMapper
						.opencity_cityname(cityname);
				if (map2 != null) {
					map3 = map2;
				}
			}
		} else {
			entity = getErrorEntity("1002");
		}
		// 将预定好的变量 放在map中 准备返回
		map.put(SendVariable.COMM_ERROR, entity);
		map.put(SendVariable.COMMON_OBJECT, map3);
		return map;
	}

	/* (non-Javadoc)
	 * @see me.send.broker.service.OpenCityService#getCityIsOpenByName(java.lang.String)
	 */
	public int getCityIsOpenByName(String cityname) {
		return opencityMapper.getCityIsOpenByName(cityname);
	}
}
